//
//  CurrencyOrderViewController.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/5/2.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class CurrencyOrderViewController: BaseViewController, SelectmangerDelegate, CurrencyOrderDelegate {
    var viewModel:CurrencyOrderViewModel?
    lazy var currencyView:CurrencyOrderView = {
        let view = CurrencyOrderView(dataSource: viewModel)
        view.delegate = self
        return view
    }()
    lazy var rightBtn:UIButton = {
        let btn = UIButton()
        btn.themeSetImage(ThemeManager.ThemeImageKeys.screen, for: UIControlState.normal)
        btn.addTarget(self, action: #selector(rightBtnAction), for: UIControlEvents.touchUpInside)
        btn.isHidden = true
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
   
        btn.bounds = CGRect(x: 0, y: 0, width: 44, height: 44)
        return btn
    }()
    
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        self.viewModel = CurrencyOrderViewModel(controller: self)
        viewModel?.type = .coinOrderType
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.title = kText("币币订单")
        view.addSubview(currencyView)
        currencyView.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.bottom.equalToSuperview().offset(0)
        }
        self.creatRightItem()
        
        viewModel?.archiveHistoryInfo(view:currencyView.historyTableView)
    }
    func creatRightItem() -> Void {
        let item  = UIBarButtonItem(customView: rightBtn)
        self.navigationItem.rightBarButtonItem = item
    }
    @objc func rightBtnAction() -> Void {
        if viewModel?.currentSelected == 0 {
            CYMBShow.showMessage(kText("当前委托不支持筛选"))
            return
        }
        SelectActionManger.manager.creatSheet(style: .selectionStyle, dataSource: self)
    }
    // MARK - SelectmangerDelegate
    func selectModel(model: SheetModel) {
        viewModel?.tradeCode = String(format:"%@/%@", model.targetCoin,model.sourceCoin)
        viewModel?.position = model.isBuyBool ? 0 : 1
        viewModel?.pageNo = 1
        viewModel?.archiveHistoryInfo(view:currencyView.historyTableView)
        
    }
    
    func changeSelectStatus(_ hiddent:Bool) {
        rightBtn.isHidden = !hiddent
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
